/*
 * addForm.java
 *
 * Created on February 22, 2006, 9:18 PM
 */

package phs_project;

import java.awt.event.KeyEvent;
import java.sql.*;
import javax.swing.JFrame;
/**
*
 * @author  Administrator
 */

public class addForm extends javax.swing.JDialog {
    
    /** Creates new form addForm */
    public addForm(JFrame parent,boolean modal) {
        super(parent,modal);
        initComponents();
        addDataComboboxType();
        //SetColHeader();
        tableData();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jPanel5 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        btExit = new javax.swing.JButton();
        btUpdate = new javax.swing.JButton();
        btAdd = new javax.swing.JButton();
        btDel = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        checkSeach = new javax.swing.JCheckBox();
        jPanel4 = new javax.swing.JPanel();
        txtName = new javax.swing.JTextField();
        txtPrice = new javax.swing.JTextField();
        lblName = new javax.swing.JLabel();
        lblPrice = new javax.swing.JLabel();
        cbbSelectType = new javax.swing.JComboBox();
        lblSelectType = new javax.swing.JLabel();
        btSeach = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        jScrollPane2.setViewportView(jTextPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Th\u00eam d\u1ecbch v\u1ee5 !");
        setAlwaysOnTop(true);
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setFocusable(false);
        btExit.setText("Tho\u00e1t");
        btExit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btExitMouseClicked(evt);
            }
        });

        btUpdate.setText("S\u1eeda");
        btUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addForm.this.actionPerformed(evt);
            }
        });

        btAdd.setText("Th\u00eam");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addForm.this.actionPerformed(evt);
            }
        });

        btDel.setText("X\u00f3a");
        btDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addForm.this.actionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(btAdd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(btUpdate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 26, Short.MAX_VALUE)
                .add(btDel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(btExit, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btAdd)
                    .add(btExit)
                    .add(btDel)
                    .add(btUpdate))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        checkSeach.setText("T\u00ecm ch\u00ednh x\u00e1c theo theo t\u00ean");
        checkSeach.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        checkSeach.setMargin(new java.awt.Insets(0, 0, 0, 0));

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(checkSeach)
                .addContainerGap(299, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(checkSeach)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        txtName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                CancelClick(evt);
                EnterClick(evt);
            }
        });

        lblName.setText("T\u00ean d\u1ecbch V\u1ee5");

        lblPrice.setText("Gi\u00e1 d\u1ecbch v\u1ee5");

        cbbSelectType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                temChanged(evt);
            }
        });
        cbbSelectType.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                EnterClick(evt);
            }
        });

        lblSelectType.setText("Ch\u1ecdn lo\u1ea1i d\u1ecbch v\u1ee5");

        btSeach.setText("T\u00ecm ");
        btSeach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addForm.this.actionPerformed(evt);
            }
        });

        jTextField1.setEnabled(false);

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, btSeach, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(lblSelectType, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(lblPrice, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(lblName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 93, Short.MAX_VALUE)
                        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, txtPrice)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, txtName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 253, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField1)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, cbbSelectType, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(lblName)
                        .add(15, 15, 15)
                        .add(lblPrice, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(14, 14, 14)
                        .add(lblSelectType))
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(txtName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(15, 15, 15)
                        .add(txtPrice, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(14, 14, 14)
                        .add(cbbSelectType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 6, Short.MAX_VALUE)
                .add(btSeach)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane1.setPreferredSize(new java.awt.Dimension(450, 400));
        table.setFont(new java.awt.Font("Times New Roman", 0, 11));
        model = new javax.swing.table.DefaultTableModel();
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table.setModel(model);
        table.setFocusable(false);// Do this only if there's no need for the component to have the focus
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(table);

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel5Layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel5Layout.createSequentialGroup()
                        .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(71, 71, 71))))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 162, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 471, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-489)/2, (screenSize.height-509)/2, 489, 509);
    }// </editor-fold>//GEN-END:initComponents

    private void EnterClick(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_EnterClick
// TODO add your handling code here:
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            btAdd.doClick();
        }
    }//GEN-LAST:event_EnterClick

    private void CancelClick(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CancelClick
// TODO add your handling code here:
        if(evt.getKeyCode()== KeyEvent.VK_ESCAPE){
            this.dispose();
        }
    }//GEN-LAST:event_CancelClick

    private void actionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionPerformed
// TODO add your handling code here:
       String nameService; 
       if(evt.getSource()==btAdd){                      
           nameService = new validate().getString(txtName.getText()); 
           if (nameService!= null){
                if(cbbSelectType.getSelectedIndex()!= 0){
                    addServices(new validate().getString(txtName.getText()),new validate().getPrice(txtPrice.getText()));
                    tableData();
                }
                else{
                    javax.swing.JOptionPane.showMessageDialog(this,"Bạn phải lựa chọn loại dịch vụ");
                    cbbSelectType.requestFocus();
                }
           }
           else{
                javax.swing.JOptionPane.showMessageDialog(this,"Tên dịch vụ không được Không được để trống");
                txtName.requestFocus();
           }
       }
       if(evt.getSource() == btDel){
            delServices();
            tableData();
       }
       if(evt.getSource()==btUpdate){
           updateService();
           tableData();
       }
       if(evt.getSource()==btSeach){
           String sql;
           if(checkSeach.isSelected()){
                if(cbbSelectType.getSelectedIndex() != 0){
                    sql = "SELECT services.name AS 'Tên dịch vụ', services.prices AS 'Giá dịch vụ', ServicesType.TypeName AS 'Loại dich vụ' ";
                    sql = sql + "FROM ServicesType INNER JOIN services ";
                    sql = sql + "ON ServicesType.TypeID = services.TypeID ";
                    sql = sql + "WHERE ServicesType.TypeName = N'"+cus_type+"'";
                    sql =  sql +" AND services.name = N'"+txtName.getText()+"'";
                    seachService(sql);
                }                    
                else if(cbbSelectType.getSelectedIndex() == 0){
                    sql = "SELECT services.name AS 'Tên dịch vụ', services.prices AS 'Giá dịch vụ', ServicesType.TypeName AS 'Loại dich vụ' ";
                    sql = sql + "FROM ServicesType INNER JOIN services ";
                    sql = sql + "ON ServicesType.TypeID = services.TypeID ";
                    sql =  sql +" WHERE services.name = N'"+txtName.getText()+"'" ;             
                    seachService(sql);
                }                          
           }
           else{
                if(cbbSelectType.getSelectedIndex() != 0){
                    sql = "SELECT services.name, services.prices, ServicesType.TypeName ";
                    sql = sql + "FROM ServicesType INNER JOIN services ";
                    sql = sql + "ON ServicesType.TypeID = services.TypeID ";
                    sql = sql + "WHERE ServicesType.TypeName = N'"+cus_type+"'";
                    sql =  sql +" AND services.name LIKE N'%"+txtName.getText()+"%'";
                    seachService(sql);
                }                    
                else if(cbbSelectType.getSelectedIndex() == 0){
                    sql = "SELECT services.name AS 'Tên dịch vụ', services.prices AS 'Giá dịch vụ', ServicesType.TypeName AS 'Loại dich vụ' ";
                    sql = sql + "FROM ServicesType INNER JOIN services ";
                    sql = sql + "ON ServicesType.TypeID = services.TypeID ";
                    sql = sql + "WHERE services.name LIKE N'%"+txtName.getText()+"%'";                                    
                    seachService(sql);
                }           
           }
       }
    }//GEN-LAST:event_actionPerformed

    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
// TODO add your handling code here:
    int selectedRow = table.getSelectedRow();
    txtName.setText(((String)table.getValueAt(selectedRow,0)));
    txtPrice.setText(((String)table.getValueAt(selectedRow,1)));
    nameService = ((String)table.getValueAt(selectedRow,0));
    }//GEN-LAST:event_tableMouseClicked

    private void temChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_temChanged
// TODO add your handling code here:
        cus_type = (String)cbbSelectType.getSelectedItem();
        if(cbbSelectType.getSelectedIndex() !=0 ){
            tableData();
        }        
    }//GEN-LAST:event_temChanged

    private void btExitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btExitMouseClicked
// TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btExitMouseClicked
    
    /**
     * @param args the command line arguments
     */

private void addDataComboboxType(){ // Function add data to combobox	
    String sql = "select  TypeName from  ServicesType";
    cbbSelectType.addItem("Lựa chọn loại dịch vụ");
    new sqlDatabase().addDataCombobox(sql,cbbSelectType);
    cbbSelectType.addItem("Tạo một loại dịch vụ mới");
}//end

private void  tableData (){
	String sql = "SELECT  services.name AS 'Tên Dịch vụ', services.prices AS 'Giá dịch vụ', ServicesType.TypeName AS 'Loại dịch vụ'";
	sql = sql + "FROM ServicesType INNER JOIN services ";
	sql = sql + "ON ServicesType.TypeID = services.TypeID ";
	sql = sql + "WHERE ServicesType.TypeName = N'"+cus_type+"'";
        new sqlDatabase().addDataTable(sql,table);
}
private void addServices(String name,int price){
    //insert into orders
    if(con == null){
        con = new connectDatabase().getConnection();
    }
    try	{
        int tID;
	String sql = "select TypeID from ServicesType";
        sql = sql + " where TypeName = N'" +cus_type+ "'";	
        Statement st = con.createStatement(); 
        ResultSet rs = st.executeQuery(sql);
	tID = 0;
	while(rs.next()){
            tID = rs.getInt(1);
	}	
        CallableStatement addServices;
        String sql2 = "Insert into services(name,prices,typeID) Values (?,?,?)"; 
        Statement sttm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        //addServices = con.prepareCall("{call addService(?,?,?)}");
        addServices = con.prepareCall(sql2);
	addServices.setString(1,name);
	addServices.setInt(2,price);
	addServices.setInt(3,tID);
	addServices.executeUpdate();
	txtName.setText("");
	txtPrice.setText("");
        sttm.close();
        rs.close();
	}
    catch(SQLException se){
	se.printStackTrace();
    }
}

private  void delServices(){
   int row;
   if(con == null){
        con = new connectDatabase().getConnection();
   }
   row = table.getSelectedRow(); 
   if(row != -1){
        try{
            Statement sttm = con.createStatement();
            String sql = "DELETE Services WHERE name = N'"+nameService+"'";
            sttm.execute(sql);
            txtName.setText("");
            txtPrice.setText("");
        }
        catch(SQLException e){
            e.printStackTrace();
        }
   }
   else{
       javax.swing.JOptionPane.showMessageDialog(this,"Bạn phải lựa chọn dịch vụ muốn sửa");
   }
}

private void seachService(String sql){
     new sqlDatabase().addDataTable(sql,table);
}  

private void updateService(){
    int row;
    String name,sql;
    if(con == null){
        con = new connectDatabase().getConnection();
    }
    row = table.getSelectedRow();
    if((row!=-1)&&(new validate().getString(txtName.getText())!=null)){      
        try{
            name = (String)table.getValueAt(row,0);
            sql = "UPDATE services SET services.name =N'"+new validate().getString(txtName.getText())+"', ";
            sql = sql + " services.prices = N'"+new validate().getPrice(txtPrice.getText())+"' ";
            sql = sql + " WHERE services.name = N'"+name+"'";            
            Statement sttm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
            int Result = sttm.executeUpdate(sql);
            if(Result >= 0 ){
                javax.swing.JOptionPane.showMessageDialog(this,"Dịch vụ đã được thay đổi");
                txtName.setText("");
                txtPrice.setText("");         
            }
        }
        catch(SQLException ce){
            System.out.println(ce);
        }
    }
    if(row == -1){
         javax.swing.JOptionPane.showMessageDialog(this,"Bạn phải lựa chọn dịch vụ muốn sửa !");
    }
}

//Main /.......
    public static void main(String args[]) {
       // java.awt.EventQueue.invokeLater(new Runnable() {
       //     public void run() {
                //new addForm().setVisible(true);
     //       }
      //  });
    }
    
    // Value user declaration 
    private Connection con;
    private String cus_type;
   
    private String nameService;
    private javax.swing.table.DefaultTableModel model = null;
    private Object[] data = null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btDel;
    private javax.swing.JButton btExit;
    private javax.swing.JButton btSeach;
    private javax.swing.JButton btUpdate;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox cbbSelectType;
    private javax.swing.JCheckBox checkSeach;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblSelectType;
    private javax.swing.JTable table;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPrice;
    // End of variables declaration//GEN-END:variables
    
}
